#include "unp.h"

void str_echo(int sockfd) {
	ssize_t n;
	char buf[MAXLINE];
	while ((n = read(sockfd, buf, MAXLINE)) > 0) {
		Writen(sockfd, buf, strlen(buf));
	}
}

int main(int argc, char **argv) {
	int listenfd, connfd;
	pid_t pid;
	struct sockaddr_in servaddr;
	listenfd = Socket(AF_INET, SOCK_STREAM, 0);
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(6666);
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	Bind(listenfd, (SA*)&servaddr, sizeof(servaddr));
	Listen(listenfd, LISTENQ);
	for (;;) {
		connfd = Accept(listenfd, NULL, NULL);
		if ((pid = Fork()) == 0) {
			Close(listenfd);
			str_echo(connfd);
			exit(0);
		}
		Close(connfd);
	}
	exit(0);
}


